﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;
using System.IO;

namespace shop.Areas.admin.Controllers
{
    [Authorize]
    public class sanphamController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/sanpham/

        // kiem tra ma san pham 
        public string checkmasp(string input)
        {
            var data = db.SanPhams.Where(s => s.MaSP == input).ToList();
            if (data.Count > 0)
            {
                return "1"; // ddax su dung 
            }
            if (data.Count == 0)
            {
                return "0";
            }
            return "";
        }
        public string checkmasp2(string input)
        {
            var data = db.SanPhams.Where(s => s.MaSP == input).ToList();
            if (data.Count > 1)
            {
                return "1"; // ddax su dung 
            }
            if (data.Count == 1)
            {
                return "0";
            }
            return "";
        }
        public ActionResult Index(int ? page=1)
        {
            int pagesize = 25;
            int pagenumber = (page ?? 1);
            var data = db.SanPhams.OrderByDescending(s=>s.ID).ToList();
            return View(data.ToPagedList(pagenumber,pagesize));
        }
        
        //
        // GET: /admin/sanpham/Details/5

        public ActionResult Details(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // GET: /admin/sanpham/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/sanpham/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(SanPham sanpham, string[] MaMau, string[] Size, HttpPostedFileBase AnhSP)
        {
            string da = DateTime.Now.ToString("yyyymmddhhMMss");
            var path = Server.MapPath("/images/sanpham/");

            if(AnhSP !=null)
            {
                sanpham.HinhAnh = da + Path.GetFileName(AnhSP.FileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                AnhSP.SaveAs(path + sanpham.HinhAnh);
            }
            else
            {
                sanpham.HinhAnh = "noimages.jpg";
            }

            //Guid s = Guid.NewGuid();
            //sanpham.MaSP = s.ToString();
            sanpham.MaSP = sanpham.MaSP.Trim();
            sanpham.HinhMau = null;
            if (ModelState.IsValid)
           {
                db.SanPhams.Add(sanpham);
                db.SaveChanges();

               // Thực hiện thêm màu sản phẩm vào bản màu
                if(MaMau != null )
                {
                    for (int i = 0; i < MaMau.Length; i++)
                    {
                        string MaM = MaMau[i];

                        SanPham_Mau m = new SanPham_Mau();
                        m.MaMau = MaM;
                        m.MaSP = sanpham.MaSP;
                        db.SanPham_Mau.Add(m);
                        db.SaveChanges();


                    }
                }
                if(Size !=null )
                {
                    for(int sk=0; sk<Size.Length;sk++)
                    {
                        SanPham_KichThuoc k = new SanPham_KichThuoc();
                        string MaS = Size[sk];
                        k.MaKT = MaS;
                        k.MaSP = sanpham.MaSP;
                        db.SanPham_KichThuoc.Add(k);
                        db.SaveChanges();
                    }
                }
                    return RedirectToAction("Index");
            }

            return View(sanpham);
        }

        //
        // GET: /admin/sanpham/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // POST: /admin/sanpham/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(SanPham sanpham, string AnhCu, HttpPostedFileBase AnhSP)
        {
            string da = DateTime.Now.ToString("yyyymmddhhMMss");
            var path = Server.MapPath("/images/sanpham/");
            if (AnhSP != null)
            {
                sanpham.HinhAnh = da + Path.GetFileName(AnhSP.FileName);
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                AnhSP.SaveAs(path + sanpham.HinhAnh);
            }
            else
            {
                sanpham.HinhAnh = AnhCu;
            }

            if (ModelState.IsValid)
            {

                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sanpham);
        }

        //
        // GET: /admin/sanpham/Delete/5

        //public ActionResult Delete(int id = 0)
        //{
        //    SanPham sanpham = db.SanPhams.Find(id);
        //    if (sanpham == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(sanpham);
        //}

        //
        // POST: /admin/sanpham/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id)
        {
            try 
            {
                SanPham sanpham = db.SanPhams.Find(id);
                db.SanPhams.Remove(sanpham);
                db.SaveChanges();
                return Content(Boolean.TrueString) ;
            }                                       
            catch
            {
                return Content(Boolean.FalseString);

            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}